package com.slt.android.androidexplorationart;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by dp on 2018/3/15.
 */

public class Other implements Parcelable {

    public String name;
    public int age;
    public boolean isMale;
    public Book book;

    protected Other(Parcel in) {
        name = in.readString();
        age = in.readInt();
        isMale = in.readByte() != 0;
        /** 由于book是一个可序列化对象，所以它的反序列化过程需要一个类加载器否则会报无法找到类的错误
         *  Android开发艺术探索中是这么写的
         *  in.readParcelable(Thread.currentThread().getContextClassLoader());
         * */
        book = in.readParcelable(Book.class.getClassLoader());
    }

    public static final Creator<Other> CREATOR = new Creator<Other>() {
        @Override
        public Other createFromParcel(Parcel in) {
            return new Other(in);
        }

        @Override
        public Other[] newArray(int size) {
            return new Other[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
        dest.writeByte((byte) (isMale ? 1 : 0));
        dest.writeParcelable(book, flags);
    }
}
